package com.wzdigit.wms.wms.task;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.basic.domain.mid.dtos.IfShipnoticeHeaderDto;
import com.wzdigit.wms.wms.constant.SyncTypeConstant;
import com.wzdigit.wms.wms.exception.SyncException;
import com.wzdigit.wms.wms.service.mid.IfShipnoticeHeaderService;
import com.wzdigit.wms.wms.service.mid.SyncErrorDataService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName IfShipnoticeToGdn
 * @Description 接口中间表-发货通知单 to 业务表 发货单，发货单明细
 * @Author LHR-PC
 * @Date 2021/3/12 13:51
 * @Version
 **/
@ConditionalOnExpression("${schedule.common.switch}")
@Component
@Slf4j
public class IfShipnoticeToGdnTask {

    @Autowired
    private IfShipnoticeHeaderService ifShipnoticeHeaderService;

    @Autowired
    private SyncErrorDataService syncErrorDataService;

    final String syncType = SyncTypeConstant.IF_SHIPNOTICE_TO_GDN;

    @Scheduled(cron = "0 0/6 * * * ?")
    public synchronized void syncData() {
        try {
            log.info("if:shipnotice => gdn start...");
            long start = System.currentTimeMillis();
            List<IfShipnoticeHeaderDto> datas = ifShipnoticeHeaderService.selectSyncData();
            if (!CollectionUtil.isEmpty(datas)) {
                syncData(datas, false);
            }
            log.info("------if:shipnotice => gdn end:{}------", System.currentTimeMillis() - start);
        } catch (RestStatusException ex) {
            ex.printStackTrace();
        }
    }


    private void syncData(List<IfShipnoticeHeaderDto> datas, boolean isSyncError) {
        for (IfShipnoticeHeaderDto data : datas) {
            try {
                log.info("-----------发货通知单begin----------单号：",data.getNoticeNo());
                ifShipnoticeHeaderService.syncData(data, isSyncError);
                log.info("---------------发货通知单success--------------");
            } catch (Exception e) {

                log.warn("发货通知单同步失败：单号{}",data.getNoticeNo());
                String message = "";
                if (SyncException.class.isInstance(e)) {
                    SyncException ex = (SyncException) e;
                    message = ex.getFailedReason();
                    if (message.length() >= 1000) {
                        message = message.substring(0, 500);
                    }
                    log.info("【发货通知单同步失败】" + ex.getFailedReason());
                    syncErrorDataService.saveSyncErrorData(ex.getSyncErrorType(), ex.getCode(),message);
                } else {
                    e.printStackTrace();
                    if (e.getMessage() == null) {
                        message = e.getClass().getName();
                    } else {
                        message = e.getMessage();
                    }
                    if (message.length() >= 1000) {
                        message = message.substring(0, 500);
                    }
                    syncErrorDataService.saveSyncErrorData(syncType, data.getNoticeNo(), message);
                }
                e.printStackTrace();
            }
        }
    }
}
